package com.cml.sqlversionupgradestarter.service;

import cn.hutool.core.date.DateTime;
import com.cml.sqlversionupgradestarter.context.SQLVersionUpgradeContext;
import com.cml.sqlversionupgradestarter.entity.SqlUpgradeVersion;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;

/**
 * 作者：陈茂林
 * 时间：2022/1/6
 * 描述：解析sql版本升级语句
 */
public class SqlUpgradeSentenceSqlExpression  implements SqlExpression{
    private Logger logger= LoggerFactory.getLogger(SqlUpgradeSentenceSqlExpression.class);
    private File file;
    private SQLVersionUpgradeContext sqlVersionUpgradeContext;
    public SqlUpgradeSentenceSqlExpression(SQLVersionUpgradeContext sqlVersionUpgradeContext,File file){
        this.file = file;
        this.sqlVersionUpgradeContext = sqlVersionUpgradeContext;
    }
    @Override
    public void interpret() {
        String fileName = file.getName();
        FileInputStream fileInputStream= null;
        try {
            fileInputStream = new FileInputStream(this.file);
            BufferedReader reader = new BufferedReader(new InputStreamReader(fileInputStream));
            String upLine="";
            SqlUpgradeVersion sqlUpgradeVersion= null;
            while (true){
                String temp = reader.readLine();
                if(temp != null){
                    if(temp.startsWith("#")){//注释

                    }else if(temp.startsWith("--")){
                        if(upLine.startsWith("--")){
                            sqlUpgradeVersion.setDesc(temp.replaceAll("--",""));
                        }else{
                            if(sqlUpgradeVersion!= null){
                                sqlVersionUpgradeContext.getSqlUpgradeContext().addSqlUpgradeContext(fileName,sqlUpgradeVersion);
                            }
                            sqlUpgradeVersion =new SqlUpgradeVersion();
                            DateTime dateTime = new DateTime(temp.replaceAll("--",""),"yyyyMMdd");
                            sqlUpgradeVersion.setVerDate(dateTime);
                        }
                        upLine=temp;
                    }else{
                        if(upLine.startsWith("--")){
                            upLine="";
                        }
                        upLine += temp;
                        if(temp.endsWith(";")){
                            sqlUpgradeVersion.getSqlList().add(upLine);
                            upLine="";//一条sql语句结束
                        }
                    }
                   //
                }else{
                    break;
                }
            }
            //处理最后一个sql升级的版本信息情况
            if(sqlUpgradeVersion!= null){
                sqlVersionUpgradeContext.getSqlUpgradeContext().addSqlUpgradeContext(fileName,sqlUpgradeVersion);
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
        }finally {

            if(fileInputStream != null){
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    logger.error(e.getMessage());
                }
            }
        }
    }
}
